package com.bchun.gamepk.dao.impl;

import java.sql.SQLException;
import java.util.Collection;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.hibernate3.HibernateCallback;

import com.bchun.gamepk.dao.IMyGamesDao;
import com.bchun.gamepk.dao.impl.BaseDaoImpl;

import com.bchun.gamepk.model.Page;
import com.bchun.gamepk.model.MyGames;

/**
 *<pre>
 * Created by Luoxudong at 2014-02-27 00:16:55
 *
 * 天天捞金
 *
 * GamePK. Copyright (C): 2014
 *
 * Description:
 *
 * Revision History
 * Date              Author        Description
 * 2014-02-27        Luoxudong     初始化设计
 *
 *</pre>
 */
public class MyGamesDaoImpl extends BaseDaoImpl<MyGames> implements
		IMyGamesDao {
		
		@Override
	public int deleteById(final Object id) throws DataAccessException {
		return getHibernateTemplate().execute(new HibernateCallback<Integer>() {

			@Override
			public Integer doInHibernate(Session session)
					throws HibernateException, SQLException {
				String hql = "delete from MyGames t where t.id in (:id)";
				Query q = session.createQuery(hql);
				if (id instanceof Collection) {
					q.setParameterList("id", (Collection<?>) id);
				} else {
					q.setParameter("id", id);
				}
				return q.executeUpdate();
			}
		});
	}

	@Override
	public Page<MyGames> selectByPage(final Page<MyGames> page) {
		return getHibernateTemplate().execute(
				new HibernateCallback<Page<MyGames>>() {

					@SuppressWarnings("unchecked")
					public Page<MyGames> doInHibernate(Session session)
							throws HibernateException, SQLException {
						String hql = "from MyGames";
						Query q = session.createQuery(hql);
						page.setTotalSize(q.list().size());
						q.setFirstResult((page.getCurrentPage() - 1)
								* page.getPageSize());
						q.setMaxResults(page.getPageSize());
						page.setRoot(q.list());
						session.flush();
						session.clear();
						return page;
					}
				});
	}
}